package top.huntwolf.admin.interceptor;

import com.github.pagehelper.Page;
import com.github.pagehelper.dialect.helper.MySqlDialect;
import org.apache.ibatis.cache.CacheKey;

import java.util.regex.Pattern;

public class PageHelperDialect extends MySqlDialect {

    @Override
    public String getPageSql(String sql, Page page, CacheKey pageKey) {

        Pattern fixed = Pattern.compile("/\\*fixed\\*/");
        String Limit = "/\\*limit\\*/";
        if (fixed.matcher(sql).find()){
            if (page.getStartRow() == 0) {
                sql = sql.replaceFirst(Limit, " LIMIT ? ");
            } else {
                sql = sql.replaceFirst(Limit, " LIMIT ?, ? ");
            }
            return sql;
        }
        return super.getPageSql(sql, page, pageKey);
    }
}
